<?php
namespace DRY\Locator;

/**
 * Direct string search locator, in which every locations is represented by a single row in a database table, containing a string as key and various fields of data
 * On successful locating, returns the values of the colums on the found row.
 * Also provides functionality to create, update and remove locations
 *
 * @author Wavetrex <wavetrex@gmail.com>
 * @link http://www.donrey.net/Documentation/Structure/Modules/locator.html
 * @license [GPL v3] http://www.gnu.org/licenses/gpl-3.0.html
 * @version 1.0.dev
 * @package DonReY
 * @subpackage Module.Locator
 *
 * @todo CRUD... but first, let's try another model
 */
class Model_Directstring extends Model_base
{

	/**
	 * Database connection module reference
	 * @var \DRY\Module\Database
	 */
	protected $_db;

	/**
	 * Queryfactory module reference
	 * @var \DRY\Module\Queryfactory
	 */
	protected $_qf;

	/**
	 * The table name where locations are found
	 * @var string
	 */
	protected $_table;

	/**
	 * The field name which contains the location
	 * @var string
	 */
	protected $_location_field;

	/**
	 * Document model constructor
	 * @param \DRY\Module\Locator $locator_module
	 * @param \SimpleXMLElement $CFG
	 * @param string $id
	 */
	public function __construct(\DRY\Module\Locator $locator_module, $CFG)
	{
		$this->_db = $locator_module[(string)$CFG-> storage-> dbDepKey];
		$this->_qf = $locator_module[(string)$CFG-> storage-> qfDepKey];
		$this->_table = (string)$CFG-> storage-> table;
		$this->_location_field = (string)($CFG-> storage['locationField']?:'location');
	}

	public function create($string, $data)
	{

	}

	/**
	 * Finds an entry in the database using a search string (usually comes from an URL)
	 * @see \DRY\Locator\Model_base::locate()
	 */
	public function locate($string)
	{
		$q = $this->_qf-> select()
			-> table($this->_table)
			-> where("`{$this->_location_field}` = ?", $string)
			-> limit(1);

		return $this->_db-> query($q)-> current();
	}

	public function update($string, $data)
	{

	}

	public function remove($string)
	{

	}

}